<?php

/*
 * +----------------------------------------------------------------------
 * 作 者: weilp
 * +----------------------------------------------------------------------
 * 创建日期：2016-08-28
 * +----------------------------------------------------------------------
 * 文件描述：
 * +----------------------------------------------------------------------
 * 升级记录：
 * +----------------------------------------------------------------------
 */
class QuestionTestEditAction extends CommonAction
{

    /**
     * 页面数据显示
     */
    public function index()
    {
        $model = new FormModel();
        
        $formid = $this->_request('formid');
        $mode = $this->_request('mode');
        $this->assign("mode", $mode);
        $this->assign("formid", $formid);
        $test_data = $model->field('id, title, intro, item_info, status')
            ->where("id = '" . $formid . "'")
            ->find();
        if (! empty($test_data['item_info'])) {
            $item_info = array();
            $item_info = explode(',', $test_data['item_info']);
            foreach ($item_info as $key => $value) {
                // 根据模块类型获取模块内容
                $item_info_detail = explode(':', $value);
                switch ($item_info_detail[0]) {
                    case 'select':
                        $data = R('Admin/QuestionTestSelect/get_item_' . $mode, array(
                            $item_info_detail[1]
                        ));
                        break;
                    case 'blank':
                        $data = R('Admin/Blank/get_item_' . $mode, array(
                            $item_info_detail[1]
                        ));
                        break;
                    case 'affix':
                        $data = R('Admin/Affix/get_item_' . $mode, array(
                            $item_info_detail[1]
                        ));
                        break;
                    case 'comSelect':
                        $data = R('Admin/ComSelect/get_item_' . $mode, array(
                            $item_info_detail[1]
                        ));
                        break;
                    default:
                        $data = array();
                }
                // dump($data);
                if (! empty($data)) {
                    $item_info[$key] = array(
                        'item_type' => $item_info_detail[0],
                        'item_id' => $item_info_detail[1],
                        'item_data' => $data
                    );
                }
            }
            $test_data['item_list'] = $item_info;
        }
        $this->assign('test_data', $test_data);
        // 获得问卷基本信息
        $this->assign('province_list', get_province_list());
        $this->assign('nation_list', get_nation_list());
        $this->assign('question_info', $this->get_form_info($this->_request('formid')));
        $this->display();
    }

    /**
     * 拖拽模块排序时更新题目显示顺序
     */
    public function update_modseq()
    {
        $item_info = trim($this->_request('item_info'));
        $testid = trim($this->_request('objid'));
        $model = new FormModel();
        $model->where("id = '" . $testid . "'")->setField('item_info', $item_info);
        $this->ajaxReturn($_POST['id'], '表单数据保存成功！', 1);
    }

    /**
     * 添加题目后更新相应的item_info字段
     * 
     * @param unknown $testid            
     * @param unknown $item_type            
     * @param unknown $item_id            
     * @return boolean
     */
    public function add_item_info($formid, $item_type, $item_id)
    {
        $model = new FormModel();
        // 活动内容序列字符串
        $old_item_info = $model->where("id = '" . $formid . "'")->getField('item_info'); // 原活动内容序列
        $add_str = $item_type . ':' . $item_id . ","; // 新增加的活动内容序列，格式为desc:id,
                                                    
        // 更新活动内容序列
        $result = $model->where("id = '" . $formid . "'")->setField('item_info', $old_item_info . $add_str);
        if (false !== $result) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 删除题目后删除相应的item_info字段
     * 
     * @param unknown $testid            
     * @param unknown $item_type            
     * @param unknown $item_id            
     * @return boolean
     */
    public function delete_item_info($formid, $item_type, $item_id)
    {
        if ($formid == '' || $item_type == '' || $item_id == '') {
            return false;
        } else {
            $model = new FormModel();
        }
        $old_item_info = $model->where("id = '" . $formid . "'")->getField('item_info'); // 获取原item_info字段值
        if (! empty($old_item_info)) {
            $item_str = $item_type . ':' . $item_id; // 要删除的item_info字段部分
                                                     
            // 在原item_info字段中查找要删除的部分，若能找到则删除(用空字符替换)
            if (false !== strpos($old_item_info, $item_str)) {
                // 字符串处理
                $mod_change = array(
                    $item_str => ''
                ); // 用空格替换要删除的部分
                $mod_clear_change = array(
                    ',,' => ','
                ); // 用一个逗号替换连续两个逗号的情况(经过上一步替换通常会出现此类情况)
                                                        
                // 字符串替换
                $new_item_info = strtr($old_item_info, $mod_change);
                $new_item_info = strtr($new_item_info, $mod_clear_change);
                $result = $model->where("id = '" . $formid . "'")->setField('item_info', $new_item_info);
                if (false !== $result) {
                    return true;
                } else {
                    return false;
                }
            }
        } else {
            return false;
        }
    }

    /**
     * 添加页面显示
     */
    public function add()
    {
        $parentid = $this->_request('parentid');
        if ($parentid == NULL) {
            $parentid = '0';
        }
        $selected = $parentid; // 当前应选中的页面
        $model = new PageModel();
        $data = $model->field('id,
								parentid,
								name,
								seq')
            ->order('seq')
            ->select();
        import('@.ORG.Tree');
        $str = "<option value='\$id' \$selected>\$spacer [\$seq] \$name</option>";
        $tree = new Tree($data);
        $categorys = $tree->get_tree('0', $str, $selected);
        $this->assign('categorys', $categorys);
        // 数据字典列表解析
        $this->assign('target_list', set_field_dict_list('page', 'target'));
        $this->assign('status_list', set_field_dict_list('page', 'status'));
        $this->display();
    }

    /**
     * 将页面数据添加至数据库
     */
    public function insert()
    {
        $model = new PageModel();
        if (false !== $model->create()) {
            if (false !== $model->add()) {
                redirect(__APP__ . '?g=Admin&m=Page&a=index');
            } else {
                $this->error('页面添加失败！');
            }
        } else {
            $this->error($model->getError());
        }
    }

    /**
     * 修改页面显示
     */
    public function edit()
    {
        $id = $this->_request('id'); // 要修改的页面的id
        $selected = $this->_request('parentid');
        $model = new PageModel();
        $data = $model->where("id = '" . $id . "'")->find();
        $this->assign('data', $data);
        $page = $model->field('id,
								parentid,
								name,
								intro,
								seq')
            ->order('seq')
            ->select();
        import('@.ORG.Tree');
        $str = "<option value='\$id' \$selected>\$spacer [\$seq] \$name</option>";
        $tree = new Tree($page);
        $categorys = $tree->get_tree('0', $str, $selected);
        $this->assign("categorys", $categorys);
        
        // 数据字典列表解析
        $this->assign('target_list', set_field_dict_list('page', 'target'));
        $this->assign('status_list', set_field_dict_list('page', 'status'));
        $this->display();
    }

    /**
     * 将页面数据更新至数据库
     */
    public function update()
    {
        $model = new PageModel();
        if (false !== $model->create()) {
            if (false !== $model->save()) {
                redirect(__APP__ . '?g=Admin&m=Page&a=index');
            } else {
                $this->error('页面修改失败！');
            }
        } else {
            $this->error($model->getError());
        }
    }

    /**
     * 删除页面数据
     */
    public function delete()
    {
        $model = new PageModel();
        if (false !== $model->where("id = '" . $this->_request('id') . "'")
            ->delete()) {
            redirect($_SERVER['HTTP_REFERER']);
        } else {
            $this->error('页面删除失败！');
        }
    }
}
?>